package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pojo.DoctorFee;
import service.DoctorFeeService;
import java.util.List;

@Api(value = "医生费用管理", description = "医院医生费用相关接口")
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/doctorfee")
public class DoctorFeeController {

    @Autowired
    private DoctorFeeService doctorFeeService;

    @ApiOperation(value = "查询所有费用项", notes = "获取所有医生收费项目信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回费用列表", response = DoctorFee.class, responseContainer = "List")
    })
    @GetMapping("/findAll")
    public ResponseEntity<List<DoctorFee>> findAll() {
        return ResponseEntity.ok(doctorFeeService.findAll());
    }

    @ApiOperation(value = "根据ID查询费用项", notes = "通过feeId获取单个收费项目信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回费用项", response = DoctorFee.class),
            @ApiResponse(code = 404, message = "未找到对应费用项")
    })
    @GetMapping("/findById/{feeId}")
    public ResponseEntity<DoctorFee> findById(@PathVariable Integer feeId) {
        DoctorFee fee = doctorFeeService.findById(feeId);
        return fee != null ? ResponseEntity.ok(fee) : ResponseEntity.notFound().build();
    }

    @ApiOperation(value = "新增费用项", notes = "创建一个新的医生收费项目")
    @ApiResponses({
            @ApiResponse(code = 201, message = "费用项创建成功"),
            @ApiResponse(code = 400, message = "无效的费用项数据")
    })
    @PostMapping("/addFee")
    public ResponseEntity<Void> addFee(@RequestBody DoctorFee fee) {
        doctorFeeService.addFee(fee);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @ApiOperation(value = "更新费用项信息", notes = "根据费用项ID更新信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "费用项信息更新成功"),
            @ApiResponse(code = 404, message = "未找到对应费用项")
    })
    @PostMapping("/updateFee")
    public ResponseEntity<Void> updateFee(@RequestBody DoctorFee fee) {
        int success = doctorFeeService.updateFee(fee);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }

    @ApiOperation(value = "删除费用项", notes = "根据费用项ID删除记录")
    @ApiResponses({
            @ApiResponse(code = 200, message = "费用项删除成功"),
            @ApiResponse(code = 404, message = "未找到对应费用项")
    })
    @DeleteMapping("/deleteFee/{feeId}")
    public ResponseEntity<Void> deleteFee(@PathVariable Integer feeId) {
        int success = doctorFeeService.deleteFee(feeId);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }
}